package com.lagou.pojo;

import com.lagou.binding.MapperRegistry;
import com.lagou.relection.factory.DefaultObjectFactory;
import com.lagou.relection.factory.ObjectFactory;
import com.lagou.sqlSession.SqlSession;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

public class Configuration {

    private DataSource dataSource;

    /*
     *   key: statementid  value:封装好的mappedStatement对象
     * */
    Map<String, MappedStatement> mappedStatementMap = new HashMap<>();

    /**
     * MapperRegistry 对象
     */
    protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
    protected ObjectFactory objectFactory = new DefaultObjectFactory();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Map<String, MappedStatement> getMappedStatementMap() {
        return mappedStatementMap;
    }

    public void setMappedStatementMap(Map<String, MappedStatement> mappedStatementMap) {
        this.mappedStatementMap = mappedStatementMap;
    }

    public MappedStatement getMappedStatement(String name) {
        return getMappedStatementMap().get(name);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return mapperRegistry.getMapper(type, sqlSession);
    }

    public <T> void addMapper(Class<T> type) {
        mapperRegistry.addMapper(type);
    }

    public ObjectFactory getObjectFactory() {
        return objectFactory;
    }
}
